import java.util.*;

public class T4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt(),m=in.nextInt(),x=0,z,index=0;
        String y;
        HashMap<Integer,String> hashMap=new HashMap<>();        //name
        HashMap<Integer,HashSet<String>> hashMap1=new HashMap<>();      //activity
        HashMap<Integer,Integer> hashMap2=new HashMap<>();      //score
        Integer[] ints;
        for (int i = 0; i < n; i++) {
            hashMap.put(in.nextInt(),in.next());
        }
        for (int i = 0; i < m; i++) {
            x=in.nextInt();
            y=in.next();
            z=in.nextInt();
            if(hashMap1.containsKey(x)) {
                hashMap1.get(x).add(y);
            }else{
                hashMap1.put(x,new HashSet<>());
                hashMap1.get(x).add(y);
            }
            hashMap2.put(x,z+hashMap2.getOrDefault(x,0));
        }
        ints=new Integer[hashMap2.size()];
        for (Integer a:hashMap2.keySet()){
            ints[index]=a;
            index++;
        }
        Arrays.sort(ints,(a,b)->{
            if(hashMap2.get(a).equals(hashMap2.get(b))){
                return hashMap.get(a).compareTo(hashMap.get(b));
            }
            return hashMap2.get(b)-hashMap2.get(a);
        });
        for (Integer a:ints) {
            if(hashMap1.get(a).size()>1){
                System.out.print(hashMap.get(a));
                System.out.println(hashMap2.get(a));
            }
        }
    }

}
